package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.emtity.Dish;
import com.itheima.reggie.emtity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*


 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;


    //添加菜品
    @Transactional
    @Override
    public void saveWithFlavors(DishDto dishDto) {
        //①. 保存菜品基本信息 ;
        this.save(dishDto);

        //②. 获取保存的菜品ID ;
        Long dishId = dishDto.getId();

        //③. 获取菜品口味列表，遍历列表，为菜品口味对象属性dishId赋值;
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            //④. 批量保存菜品口味列表;
            dishFlavorService.save(flavor);
        }
    }

    //根据Id查询
    @Override
    public DishDto getByIDWithFlavor(Long id) {
        //根据菜品id获取dish信息
        Dish dish = this.getById(id);
        //拷贝数据,此时数据中没有口味信息
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //从dish_flavor中根据菜品ID查询口味信息
        //select * from dish_flavor where dish_id = ''
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lqw);
        //将口味信息封装到dishDto
        dishDto.setFlavors(list);
        return dishDto;
    }

    //修改菜品
    @Transactional
    @Override
    public void updateWithFlavors(DishDto dishDto) {
        log.info(dishDto.toString());
        this.updateById(dishDto);
        //清理dish_flavor中的口味
        //delete from dish_flavor  where dish_id = ''
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);

        //新增口味
        //insert into dish_flavor values()
        /*List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //批量保存口味数据
        dishFlavorService.saveBatch(flavors);*/

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        }
    }
	
	
	 //删除
    @Transactional
    @Override
    public void deleteWithFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //查询该菜品是否再售卖，若售卖，则抛出异常
        //select * from dish where id in  () and status =1
        lqw.in(Dish::getId,ids);
        lqw.eq(Dish::getStatus,1);
        int count = this.count(lqw);
        //count>0，说明其中有裁菜品还在售卖中，不能删除
        if(count > 0){
            throw new CustomException("有菜品还在售卖中，不能删除");
        }
        //说明选中的菜品都是停售状态
        //删除dish中的数据
        this.removeByIds(ids);
        //同时删除口味表中的数据
        LambdaQueryWrapper<DishFlavor> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw2);
    }


}
